package com.gator.rocketmq.consumer;

import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.client.consumer.DefaultMQPushConsumer;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyContext;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyStatus;
import org.apache.rocketmq.client.consumer.listener.MessageListenerConcurrently;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.common.message.MessageExt;
import org.apache.rocketmq.common.protocol.heartbeat.MessageModel;

import java.util.List;

/**
 * @Author PABLO
 * @Date 2022/6/7 21:56
 * @Desc 集群消费模式
 */
@Slf4j
public class Consumer {

    public static void main(String[] args) throws MQClientException {
        DefaultMQPushConsumer consumer = new DefaultMQPushConsumer("consumer_group");

        consumer.setNamesrvAddr("localhost:9876");

        //*指定订阅的tag
        consumer.subscribe("TopicTest", "*");
        //默认集群模式
        consumer.setMessageModel(MessageModel.CLUSTERING);
        //回调
        consumer.registerMessageListener(new MessageListenerConcurrently() {
            @Override
            public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> msgs, ConsumeConcurrentlyContext context) {

                try {
                    for (MessageExt item : msgs
                    ) {
                        String topic = item.getTopic();
                        String msg = new String(item.getBody(), "UTF-8");
                        String tags = item.getTags();
                        log.info("收到消息{}，tag是：{}，topic是：{}", msg, tags, topic);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                    return ConsumeConcurrentlyStatus.RECONSUME_LATER;
                }
                return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
            }
        });

        consumer.start();

    }
}
@Slf4j
class Consumer2 {

    public static void main(String[] args) throws MQClientException {
        DefaultMQPushConsumer consumer = new DefaultMQPushConsumer("consumer_group2");

        consumer.setNamesrvAddr("localhost:9876");

        //*指定订阅的tag
        consumer.subscribe("TopicTest", "*");
        //默认集群模式
        consumer.setMessageModel(MessageModel.CLUSTERING);
        //回调
        consumer.registerMessageListener(new MessageListenerConcurrently() {
            @Override
            public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> msgs, ConsumeConcurrentlyContext context) {

                try {
                    for (MessageExt item : msgs
                    ) {
                        String topic = item.getTopic();
                        String msg = new String(item.getBody(), "UTF-8");
                        String tags = item.getTags();
                        log.info("收到消息{}，tag是：{}，topic是：{}", msg, tags, topic);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                    return ConsumeConcurrentlyStatus.RECONSUME_LATER;
                }
                return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
            }
        });

        consumer.start();

    }
}

